"""
   ┌─┐       ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│                 │
│       ───       │++ + + +
███████───███████ │+
│                 │+
│       ─┴─       │
│                 │
└───┐         ┌───┘
    │         │
    │         │   + +
    │         │
    │         └──────────────┐
    │                        │
    │                        ├─┐
    │                        ┌─┘
    │                        │
    └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
      │ ─┤ ─┤       │ ─┤ ─┤
      └──┴──┘       └──┴──┘  + + + +
             神兽保佑
            代码无BUG!
"""
import os

import pandas as pd


class WordImg:
    def __init__(self):
        self.word_img = {}
        self.all_content = []
        self.topic_Frame = pd.DataFrame(columns=["id", "img_url", "content", "tags", "img_number"])

    def read_text(self):
        content_file = pd.read_excel("./微博.xlsx", sheet_name="微博内容")
        # 读取文件所有标签
        for item in content_file.values:
            self.all_content.append(item)
            self.word_img[item[0]] = []
        print(len(self.all_content))

    def read_img(self):
        for img in os.listdir("./new_file"):
            try:
                key = int(img.split("_")[0])
            except:
                continue
            if key in self.word_img.keys():
                self.word_img[key].append(img)
        print(len(self.word_img))

    def write_text(self):
        for content in self.all_content:
            img = ",".join(self.word_img[content[0]])
            self.topic_Frame = self.topic_Frame.append(pd.DataFrame({
                "id": [content[0]], "img_url":[content[1]],"content": [content[2]], "tags": [content[3]],
                "img_number": [img]
            }))  # ,
        self.topic_Frame.to_excel("new_weibo_1.xlsx", "Sheet1", index=False)

    def run(self):
        self.read_text()
        self.read_img()
        self.write_text()


if __name__ == '__main__':
    w = WordImg()
    w.run()
